﻿/*
Copyright (c) 2010, hiSoft
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the hiSoft nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE HISOFT AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL HISOFT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
using System;
using System.Threading;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Orchard.Test.Library;
using Selenium;

namespace Orchard.Test.WorkThoughCases.Auto
{
    [TestClass]
    public class DoActionToBlogAndBlogPost
    {
        private TestLibrary TestLibrary;
        private ISelenium selenium;

        [TestInitialize]
        public void SetupTest()
        {
            TestLibrary = TestLibrary.SetupTest(this.ToString());
            selenium = TestLibrary.Selenium;
        }

        [TestCleanup]
        public void TeardownTest()
        {
            TestLibrary.ShutDown();
        }

        [TestMethod]
        public void WorkThoughCases_DoActionToBlogAndBlogPost()
        {
            TestLibrary.UserHelper.LogOnAsAdmin();
            BlogSettings blogSetting = new BlogSettings();
            blogSetting.Description = "Do Action to Blog";
            blogSetting.OnAdminMenu = true;
            blogSetting.AdminMenuText = "BlogOfDoAction";
            blogSetting.AdminMenuPosition = "1";
            blogSetting.OnMainMenu = true;
            blogSetting.MenuText = "DoActionToBlog";
            
            var blog=TestLibrary.BlogHelper.CreateBlog(blogSetting);        
            if (TestLibrary.MessagesHelper.GetErrorInfoItemsCount() > 0 || TestLibrary.MessagesHelper.GetValidateErrorInfoItemsCount()>0)
                Assert.Fail("Note:bug#local01");
            selenium.Open("/"+blog.Permalink);

            PostSettings postSetting=new PostSettings();
            Tag tag=new Tag("Orchard tag",null);
            Tag tag2=new Tag(String.Format("Tag{0}", UIHelperBase.GenerateUniqueIdentifier(DateTime.Now)), null);
            postSetting.Tags=new List<Tag>(){tag,tag2};
            var post=blog.CreatePost(postSetting);
            if (TestLibrary.MessagesHelper.GetErrorInfoItemsCount() > 0 || TestLibrary.MessagesHelper.GetValidateErrorInfoItemsCount() > 0)
                Assert.Fail("Note:bug#local02");
            selenium.Open("/"+blog.Permalink+"/"+postSetting.Permalink);

            TestLibrary.BlogHelper.ManageBlogs();
            //blog.DoActionToPost(post, PostAction.Edit);
            Tag newTag = new Tag("Post edited", null);
            PostSettings newPostSettings= new PostSettings() { Title = post.Title + "_Edited", Permalink = post.Permalink + "_Edited",Tags=new List<Tag>{tag,tag2,newTag},PublishSetting=PublishSettings.SaveDraft};
            //need edit content operations
            post= blog.EditPost(post, newPostSettings);

            PostSettings newPostSetting2 = newPostSettings;
            newPostSetting2.PublishSetting = PublishSettings.PublishLater;
            DateTime publish = DateTime.Now + TimeSpan.FromMinutes(1);
            newPostSetting2.PublishDate = TestLibrary.GetScheduledDateString(publish);
            newPostSetting2.PublishTime = TestLibrary.GetScheduledTimeString(publish);
            blog.EditPost(post, newPostSetting2);
            Thread.Sleep(60000);
            TestLibrary.BlogHelper.DoActionToBlog(TestLibrary.DefaultBlog, BlogAction.ListPosts);
            Assert.AreEqual("Published", blog.GetPublishStatusOfPost(post), "bug#199");

            blog.DoActionToPost(post, PostAction.Unpublish);
            Assert.AreEqual(blog.GetPublishStatusOfPost(post), "Not Published");
            blog.DoActionToPost(post, PostAction.Publish);
            blog.DoSelectActionToPost(post, PostAction.Unpublish);
            blog.DoSelectActionToPost(post, PostAction.Publish);
            Assert.AreEqual(blog.GetPublishStatusOfPost(post), "Published");
            blog.DoSelectActionToPost(post, PostAction.Delete);
            Assert.IsFalse(blog.IsPostPresent(post), "Note:bug#local03");
            var post2 = blog.CreateRandomPost();
            blog.DoActionToPost(post2, PostAction.Delete);
            Assert.IsFalse(blog.IsPostPresent(post2), "Note:bug#local04");

            selenium.Open("/Admin/Blogs");
            TestLibrary.BlogHelper.DoActionToBlog(blog, BlogAction.ListPosts);
            Thread.Sleep(3000);
            selenium.Click("link=Blog Properties");
            BlogSettings newBlogSettings = new BlogSettings();  //error: newBlogSettings=blogSetting why?            
            newBlogSettings.Title = blogSetting.Title + "_Edited";           
            newBlogSettings.Description = "Do Action to Blog _Edited ";
            newBlogSettings.OnAdminMenu = true;
            newBlogSettings.AdminMenuText = "BlogOfDoActionEdited";
            newBlogSettings.AdminMenuPosition = "1";
            newBlogSettings.OnMainMenu = true;
            newBlogSettings.MenuText = "DoActionToBlogEdited";
            blog= TestLibrary.BlogHelper.EditBlog(blog, newBlogSettings);

            selenium.Open("/Admin/Blogs");
            TestLibrary.BlogHelper.DoActionToBlog(blog, BlogAction.Unpublish);
            Assert.AreEqual(TestLibrary.BlogHelper.GetPublishStatusOfBlog(blog), "Unpublish");
            TestLibrary.BlogHelper.DoActionToBlog(blog, BlogAction.Publish);
            Assert.AreEqual(TestLibrary.BlogHelper.GetPublishStatusOfBlog(blog), "Publish");

            TestLibrary.BlogHelper.SelectDeleteBlog(blog);
            Assert.IsFalse(TestLibrary.BlogHelper.IsBlogPresent(blog),"Note:bug#local05");
            var blog2=TestLibrary.BlogHelper.CreateRandomBlog();
            TestLibrary.BlogHelper.DoActionToBlog(blog2, BlogAction.Delete);
            Assert.IsFalse(TestLibrary.BlogHelper.IsBlogPresent(blog2),"Note:bug#local06");

        }
    }
}
